package com.sxkiler.demo.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

@RequestMapping("/html")
@Controller
public class HtmlController {

    @RequestMapping("/redirect")
    public void redirect(@RequestParam String location, HttpServletRequest request, HttpServletResponse response) throws IOException {
//        response.setContentType("application/octet-stream");
        if(location!=null){
            if(location.startsWith("http")){
                //重定向外部页面
                response.sendRedirect(location);
            } else {
                //重定向内部页面
                response.setStatus(302);//或者303,兼容http1.1
                if (!location.startsWith("/")) {
                    location = request.getContextPath() + "/" +location;
                }
                response.setHeader("location", location);
            }
        }
    }

    @RequestMapping("/returnHtml")
    public void returnHtml(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/octet-stream");
        StringBuffer buffer = new StringBuffer("<!DOCTYPE HTML>\n");
        buffer.append("<html>\n");
buffer.append("\t<head>\n");
buffer.append("\t\t<meta charset=\"utf-8\">\n");
buffer.append("\t\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0\"/>\n");
buffer.append("\t\t<meta name=\"format-detection\" content=\"telephone=no\"/>\n");
buffer.append("\t\t<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\" />\n");
buffer.append("\t\t<meta http-equiv=\"Expires\" content=\"0\">\n");
buffer.append("\t\t<meta http-equiv=\"Pragma\" content=\"no-cache\">\n");
buffer.append("\t\t<meta http-equiv=\"Cache-control\" content=\"no-cache,no-store\">\n");
buffer.append("\t\t<meta http-equiv=\"Cache\" content=\"no-cache\">\n");
buffer.append("\t\t<style type=\"text/css\">\n");
buffer.append("\t\t.error-center-box\n");
buffer.append("\t\t{\n");
buffer.append("\t\t\tposition: absolute;\n");
buffer.append("\t\t\ttop: 0;\n");
buffer.append("\t\t\tbottom: 0;\n");
buffer.append("\t\t\tleft: 0;\n");
buffer.append("\t\t\tright: 0;\n");
buffer.append("\t\t\tmargin: 0px auto;\n");
buffer.append("\t\t\tbackground: #ffffff;\n");
buffer.append("\t\t}\n");
buffer.append("\t\t.error-center\n");
buffer.append("\t\t{\n");
buffer.append("\t\t\tmax-width: 400px;\n");
buffer.append("\t\t\theight: 370px;\n");
buffer.append("\t\t}\n");
buffer.append("\t\t.error-sorry-img\n");
buffer.append("\t\t{\n");
buffer.append("\t\t\twidth: 200px;\n");
buffer.append("\t\t\theight: 200px;\n");
buffer.append("\t\t\tmargin: 0px auto;\n");
buffer.append("\t\t}\n");
buffer.append("\t\t.error-msg\n");
buffer.append("\t\t{\n");
buffer.append("\t\t\tpadding-top: 6px;\n");
buffer.append("\t\t\tmargin: 0px auto;\n");
buffer.append("\t\t\ttext-align: center;\n");
buffer.append("\t\t\tcolor: #666666;\n");
buffer.append("\t\t\tword-break: break-all;\n");
buffer.append("\t\t}\n");
buffer.append("\t\t</style>\n");
buffer.append("\t</head>\n");
buffer.append("\t<body onload=\"funInit()\" style=\"margin:0\">\n");
buffer.append("\t\t<div class=\"error-center-box\">\n");
buffer.append("\t\t\t<div class=\"error-center\">\n");
buffer.append("\t\t\t\t<div class=\"error-msg\">\n");
buffer.append("\t\t\t\t\t<div id=\"error-msg-text\">中文测试</div>\n");
buffer.append("\t\t\t\t</div>\n");
buffer.append("\t\t\t</div>\n");
buffer.append("\t\t</div>\n");
buffer.append("\t</body>\n");
buffer.append("</html>");
        OutputStream out = response.getOutputStream();
        out.write(buffer.toString().getBytes());
        out.flush();
        out.close();
        
    }
}
